#!/hint/zsh
emulate -LR zsh -o extended_glob -o err_return

local zsh_version=${1:-${FZF_TAB_ZSH_SRC_VERSION:-$ZSH_VERSION}}

# macos check
local ret bundle nproc
[[ $OSTYPE == darwin* ]] && {
  [[ -n ${module_path[1]}/**/*.bundle(#qN) ]] && bundle=true
  nproc=$(sysctl -n hw.logicalcpu)
} || {
  nproc=$(nproc)
}

# clone zsh source code if not exists
[[ -d ./zsh/$zsh_version ]] || {
  git clone --depth=1 --branch zsh-$zsh_version https://github.com/zsh-users/zsh ./zsh/$zsh_version
}

ln -sf $PWD/Src/fzftab.c   ./zsh/$zsh_version/Src/Modules/
ln -sf $PWD/Src/fzftab.mdd ./zsh/$zsh_version/Src/Modules/

# build zsh
cd -q ./zsh/$zsh_version

git checkout -- .

[[ $zsh_version != "5.9" ]] || {
  curl -s https://github.com/zsh-users/zsh/commit/4c89849c98172c951a9def3690e8647dae76308f.patch | git apply --exclude=ChangeLog -
  curl -s https://github.com/zsh-users/zsh/commit/ab4d62eb975a4c4c51dd35822665050e2ddc6918.patch | git apply --exclude=ChangeLog -
}

[[ -f ./configure ]] || ./Util/preconfig
[[ -f ./Makefile ]] || ./configure --disable-gdbm --disable-pcre --without-tcsetpgrp --prefix=/tmp/zsh-fzf-tab-module ${bundle:+DL_EXT=bundle}
make -j$nproc

# we only need aloxaf/fzftab.so
mv ./Src/Modules/fzftab.(so|bundle) $FZF_TAB_HOME/modules/Src/aloxaf/
